﻿<template>
  <div class="app-container">
    <div class="filter-title">
      <el-form label-position="right"
               label-width="95px">
        <el-form-item :label="$t('rule.code')+' :'">
          <el-input v-model="listQuery.name"
                    style="width: 150px"
                    class="filter-item"
                    :placeholder="$t('rule.code')"
                    @keyup.enter.native="handleFilter"></el-input>
        </el-form-item>
        <el-form-item :label="$t('rule.equipmentCode')+' :'">
          <el-input v-model="listQuery.equipment_code"
                    style="width: 150px"
                    class="filter-item"
                    :placeholder="$t('rule.equipmentCode')"
                    @keyup.enter.native="handleFilter"></el-input>
        </el-form-item>
        <el-form-item :label="$t('rule.videoName')+' :'">
          <el-input v-model="listQuery.video_name"
                    style="width: 150px"
                    class="filter-item"
                    :placeholder="$t('rule.videoName')"
                    @keyup.enter.native="handleFilter"></el-input>
        </el-form-item>
        <el-button style="margin-left:20px"
                   class="filter-item"
                   type="primary"
                   icon="el-icon-search"
                   @click="handleFilter">{{ $t('table.search') }}</el-button>
        <el-button v-if="permission.add"
                   style="margin-left: 10px;float:right"
                   class="filter-item"
                   type="primary"
                   icon="el-icon-edit"
                   @click="handleAddRule('', 'createRule')">{{ $t('table.add') }}</el-button>
        <el-button style="margin-left: 10px"
                   class="filter-item"
                   type="primary"
                   @click="handleRefresh">{{ $t('table.refresh') }}</el-button>
        <div v-show="!showSearchItem"
             class="showDown"
             @click="showSearchItem = true">
          <i class="el-icon-d-arrow-right"></i>
        </div>
        <div v-show="showSearchItem"
             class="showUp"
             @click="showSearchItem = false">
          <i class="el-icon-d-arrow-left"></i>
        </div>
      </el-form>
    </div>
    <div v-show="showSearchItem"
         class="filter-container">
      <el-form label-position="right"
               label-width="95px">
        <el-form-item v-if="type === 1"
                      :label="$t('rule.adName')+' :'">
          <el-input v-model="listQuery.ad_name"
                    style="width: 150px"
                    class="filter-item"
                    :placeholder="$t('rule.adName')"
                    @keyup.enter.native="handleFilter"></el-input>
        </el-form-item>
        <el-form-item v-if="type !== 3"
                      :label="$t('public.region')+' :'">
          <el-input v-model="listQuery.region_name"
                    style="width: 150px"
                    class="filter-item"
                    :placeholder="$t('public.region')"
                    @keyup.enter.native="handleFilter"></el-input>
        </el-form-item>
        <el-form-item v-if="type !== 3"
                      :label="$t('public.trade')+' :'">
          <el-input v-model="listQuery.trade_name"
                    style="width: 150px"
                    class="filter-item"
                    :placeholder="$t('public.trade')"
                    @keyup.enter.native="handleFilter"></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.checkStatus')+' :'">
          <el-select v-model="listQuery.check_status"
                     clearable
                     style="width: 150px"
                     class="filter-item"
                     :placeholder="$t('public.checkStatus')">
            <el-option v-for="item in statusOptions"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('rule.checkCoupon')+' :'">
          <el-select v-model="listQuery.is_coupon"
                     clearable
                     style="width: 150px"
                     class="filter-item"
                     :placeholder="$t('rule.checkCoupon')">
            <el-option v-for="item in couponOptions"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('rule.time')+' :'">
          <div class="filter-item">
            <el-date-picker v-model="listQuery.startTime"
                            :picker-options="pickerOptionsB"
                            type="date"
                            :placeholder="$t('rule.startTime')"
                            value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </div>
          <div class="filter-item">
            <el-date-picker v-model="listQuery.endTime"
                            :picker-options="pickerOptionsE"
                            type="date"
                            :placeholder="$t('rule.endTime')"
                            value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <el-table :key="tableKey"
              v-loading="listLoading"
              :data="list"
              element-loading-text="拼命加载中"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255,255,255,0.2)"
              border
              fit
              highlight-current-row
              style="width: 100%min-height:1000px">
      <el-table-column fixed="left"
                       align="center"
                       :label="$t('table.id')"
                       width="65">
        <template slot-scope="scope">
          <span>{{ (listQuery.page-1)*listQuery.limit+scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center"
                       show-overflow-tooltip
                       :label="$t('rule.name')">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center"
                       show-overflow-tooltip
                       :label="$t('rule.adName')">
        <template slot-scope="scope">
          <span>{{ scope.row.ad_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center"
                       show-overflow-tooltip
                       :label="$t('rule.videoName')">
        <template slot-scope="scope">
          <span>{{ scope.row.video_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center"
                       show-overflow-tooltip
                       :label="$t('rule.addTime')">
        <template slot-scope="scope">
          <span>{{ scope.row.add_time }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center"
                       show-overflow-tooltip
                       :label="$t('public.region')">
        <template slot-scope="scope">
          <span>{{ scope.row.region_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center"
                       show-overflow-tooltip
                       :label="$t('public.trade')">
        <template slot-scope="scope">
          <span>{{ scope.row.trade_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center"
                       :label="$t('public.checkStatus')">
        <template slot-scope="scope">
          <span :class="statusColorSecond(scope.row.check_status)">{{ statusEsSecond(scope.row.check_status) }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right"
                       align="center"
                       :label="$t('table.actions')"
                       :width="operWidth"
                       class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary"
                     size="mini"
                     @click="handleRead(scope.row)">{{ $t('table.read') }}</el-button>
          <el-button v-if="permission.update"
                     type="primary"
                     size="mini"
                     @click="handleAddRule(scope.row, 'update')">{{ $t('table.edit') }}</el-button>
          <el-button v-if="permission.review"
                     type="primary"
                     size="mini"
                     @click="handleReview(scope.row)">{{ $t('table.review') }}</el-button>
          <el-button v-if="permission.del"
                     size="mini"
                     type="danger"
                     @click="handleModifyStatus(scope.row,'deleted')">{{ $t('table.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background
                     :current-page="listQuery.page"
                     :page-sizes="[10,20,30,50]"
                     :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"></el-pagination>
    </div>
    <!-- 编辑 -->
    <el-dialog :title="textMap[dialogStatus]"
               :visible.sync="dialogFormVisible"
               width="950px">
      <el-form ref="dataForm"
               :rules="rules"
               :model="temp"
               label-position="right"
               label-width="35%"
               style="width: 400px margin-left:120px"
               size="small">
        <div class="pops">
          <div class="popUp left">
            <div style="width:20%;text-align:right">
              <span class="information-title">基础规则:</span>
            </div>
            <el-form-item :label="$t('rule.name')+' :'">
              <span>{{ temp.name }}</span>
            </el-form-item>
            <el-form-item :label="$t('rule.adName')+' :'">
              <span>{{ temp.ad_name }}</span>
            </el-form-item>
            <el-form-item :label="$t('rule.videoName')+' :'">
              <span>{{ temp.video_name }}</span>
            </el-form-item>
            <el-form-item :label="$t('rule.data')+' :'">
              <span>{{ getData(temp.start_time) }}&nbsp;至&nbsp;{{ getData(temp.end_time) }}</span>
            </el-form-item>
            <el-form-item :label="$t('rule.time')+' :'">
              <span>{{ getRuleTime(temptime) }}</span>
            </el-form-item>
            <el-form-item :label="$t('public.region')+' :'">
              <span>{{ temp.region_name }}</span>
            </el-form-item>
            <el-form-item :label="$t('rule.playlimit')+' :'">
              <span>{{ temp.play_limit }}</span>
            </el-form-item>
            <el-form-item :label="$t('rule.adWord')+' :'">
              <span v-if="temp.ad_word">{{ temp.ad_word }}</span>
              <span v-else>无广告词</span>
            </el-form-item>
            <el-form-item v-if="temp.describe"
                          :label="$t('rule.describe')+' :'">
              <span>{{ temp.describe }}</span>
            </el-form-item>
            <el-form-item v-if="is_coupon===1"
                          :label="$t('rule.couponUrl')+' :'">
              <span>{{ temp.coupon_url }}</span>
            </el-form-item>
            <el-form-item v-if="is_coupon===1 && temp.coupon_explain"
                          :label="$t('rule.couponExplain')+' :'">
              <span>{{ temp.coupon_explain }}</span>
            </el-form-item>
            <el-form-item v-if="is_coupon===1"
                          :label="$t('rule.buyAward')+' :'">
              <span>{{ temp.buy_award }}</span>
            </el-form-item>
            <el-form-item v-if="is_coupon===1"
                          :label="$t('rule.couponAward')+' :'">
              <span>{{ temp.coupon_award }}</span>
            </el-form-item>
            <el-form-item v-if="dialogStatus=='review'"
                          :label="$t('public.checkStatus')+' :'"
                          prop="check_status">
              <el-radio-group v-model="temp.check_status">
                <el-radio :label="0"
                          disabled>审核中</el-radio>
                <el-radio :label="1">审核通过</el-radio>
                <el-radio :label="2">未通过</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item v-if="temp.check_status === 2"
                          :label="$t('public.auditOpinion')+' :'"
                          prop="check_remark">
              <el-input v-model="temp.check_remark"
                        type="textarea"></el-input>
            </el-form-item>
          </div>
          <div class="popUp right">
            <span class="information-title">精准投放:</span>
            <el-form-item v-if="temp.timing"
                          :label="$t('rule.timing')+' :'">
              <span>{{ whetherEs(temp.timing) }}</span>
            </el-form-item>
            <el-form-item v-if="type !==3 && temp.trade_name"
                          :label="$t('public.trade')+' :'">
              <span>{{ temp.trade_name }}</span>
              <el-checkbox v-if="temp.trade_status===2"
                           v-model="temp.trade_status"
                           style="margin-left:10px"
                           :true-label="2"
                           :false-label="1"
                           disabled>排斥</el-checkbox>
            </el-form-item>
            <el-form-item v-if="temp.circle_name"
                          :label="$t('rule.businessDistrict')+' :'">
              <span>{{ temp.circle_name }}</span>
            </el-form-item>
            <el-form-item v-if="temp.equipment_type_name"
                          :label="$t('equipment.type')+' :'">
              <span>{{ temp.equipment_type_name }}</span>
            </el-form-item>
            <el-form-item v-if="temp.merchant_name"
                          :label="$t('rule.designatedMerchant')+' :'">
              <span>{{ temp.merchant_name }}</span>
            </el-form-item>
            <el-form-item v-if="temp.equipment_name"
                          :label="$t('rule.equipmentCode')+' :'">
              <span>{{ temp.equipment_name }}</span>
            </el-form-item>
            <el-form-item v-if="temp.bidding>1"
                          :label="$t('rule.bidding')+' :'">
              <span>+{{ (temp.bidding-1).toFixed(1)*100 }}%</span>
            </el-form-item>
            <el-form-item :label="$t('forecast.count')+' :'">
              <span>{{ temp.equipment_count }}</span>
            </el-form-item>
            <el-form-item :label="$t('forecast.money')+' :'">
              <span>{{ temp.count_money }}</span>
            </el-form-item>
          </div>
        </div>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button v-if="dialogStatus=='review'"
                   type="primary"
                   :disabled="reviewDisabled"
                   @click="reviewData">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>

    <!-- 查看 -->
    <el-dialog :title="textMap[dialogStatus]"
               :visible.sync="dialogFormRead"
               width="950px">
      <el-form :model="temp"
               label-position="right"
               label-width="40%"
               style="width: 400px margin-left:120px">
        <div class="pops">
          <div class="popUp left">
            <div style="width:20%;text-align:right">
              <span class="information-title">基础规则:</span>
            </div>
            <el-form-item :label="$t('rule.name')+' :'">
              <span>{{ temp.name }}</span>
            </el-form-item>
            <!-- <el-form-item :label="$t('rule.adName')+' :'">
              <span>{{  temp.ad_name  }}</span>
            </el-form-item>-->
            <el-form-item :label="$t('rule.videoName')+' :'">
              <span>{{ temp.video_name }}</span>
            </el-form-item>
            <el-form-item :label="$t('rule.data')+' :'">
              <span>{{ getData(temp.start_time) }}&nbsp;至&nbsp;{{ getData(temp.end_time) }}</span>
            </el-form-item>
            <el-form-item :label="$t('rule.time')+' :'">
              <span>{{ getRuleTime(temptime) }}</span>
            </el-form-item>
            <el-form-item :label="$t('public.region')+' :'">
              <span>{{ temp.region_name }}</span>
            </el-form-item>
            <el-form-item :label="$t('rule.playlimit')+' :'">
              <span>{{ temp.play_limit }}</span>
            </el-form-item>
            <el-form-item :label="$t('rule.adWord')+' :'">
              <span v-if="temp.ad_word">{{ temp.ad_word }}</span>
              <span v-else>无广告词</span>
            </el-form-item>
            <el-form-item v-if="temp.describe"
                          :label="$t('rule.describe')+' :'">
              <span>{{ temp.describe }}</span>
            </el-form-item>
            <el-form-item v-if="is_coupon===1"
                          :label="$t('rule.couponUrl')+' :'">
              <span>{{ temp.coupon_url }}</span>
            </el-form-item>
            <el-form-item v-if="is_coupon===1 && temp.coupon_explain"
                          :label="$t('rule.couponExplain')+' :'">
              <span>{{ temp.coupon_explain }}</span>
            </el-form-item>
            <el-form-item v-if="is_coupon===1"
                          :label="$t('rule.buyAward')+' :'">
              <span>{{ temp.buy_award }}</span>
            </el-form-item>
            <el-form-item v-if="is_coupon===1"
                          :label="$t('rule.couponAward')+' :'">
              <span>{{ temp.coupon_award }}</span>
            </el-form-item>
            <el-form-item :label="$t('public.checkStatus')+' :'"
                          prop="check_status">
              <span>{{ statusEsSecond(temp.check_status) }}</span>
            </el-form-item>
            <el-form-item v-if="temp.check_status === 2"
                          :label="$t('public.auditOpinion')+' :'">
              <span>{{ temp.check_remark }}</span>
            </el-form-item>
          </div>
          <div class="popUp right">
            <span class="information-title">精准投放:</span>
            <el-form-item v-if="temp.timing"
                          :label="$t('rule.timing')+' :'">
              <span>{{ whetherEs(temp.timing) }}</span>
            </el-form-item>
            <el-form-item v-if="type !==3 && temp.trade_name"
                          :label="$t('public.trade')+' :'">
              <span>{{ temp.trade_name }}</span>
              <el-checkbox v-if="temp.trade_status===2"
                           v-model="temp.trade_status"
                           style="margin-left:10px"
                           :true-label="2"
                           :false-label="1"
                           disabled>排斥</el-checkbox>
            </el-form-item>
            <el-form-item v-if="temp.circle_name"
                          :label="$t('rule.businessDistrict')+' :'">
              <span>{{ temp.circle_name }}</span>
            </el-form-item>
            <el-form-item v-if="temp.equipment_type_name"
                          :label="$t('equipment.type')+' :'">
              <span>{{ temp.equipment_type_name }}</span>
            </el-form-item>
            <el-form-item v-if="temp.merchant_name"
                          :label="$t('rule.designatedMerchant')+' :'">
              <span>{{ temp.merchant_name }}</span>
            </el-form-item>
            <el-form-item v-if="temp.equipment_name"
                          :label="$t('rule.equipmentCode')+' :'">
              <span>{{ temp.equipment_name }}</span>
            </el-form-item>
            <el-form-item v-if="temp.bidding>1"
                          :label="$t('rule.bidding')+' :'">
              <span>+{{ (temp.bidding-1).toFixed(1)*100 }}%</span>
            </el-form-item>
            <el-form-item :label="$t('forecast.count')+' :'">
              <span>{{ temp.equipment_count }}</span>
            </el-form-item>
            <el-form-item :label="$t('forecast.money')+' :'">
              <span>{{ temp.count_money }}</span>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getToken } from "@/utils/auth"; // get token from cookie

export default {
  name: "PlayRuleAppalication",
  data() {
    const remark = (rule, value, callback) => {
      if (value) {
        if (value.length <= 0) {
          callback(new Error("请输入审核意见"));
        } else if (value.length > 300) {
          callback(new Error("审核意见不能超过300个字符"));
        } else {
          callback();
        }
      } else {
        callback(new Error("请输入审核意见"));
      }
    };
    return {
      showSearchItem: false,
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      type: "",
      statusOptions: [
        {
          value: 0,
          label: "审核中"
        },
        {
          value: 2,
          label: "未通过"
        }
      ],
      couponOptions: [
        {
          value: 1,
          label: "含优惠券"
        },
        {
          value: 2,
          label: "不含优惠券"
        }
      ],
      listQuery: {
        page: 1,
        limit: 20,
        name: "",
        ad_name: "",
        video_name: "",
        region_name: "",
        trade_name: "",
        equipment_code: "",
        check_status: "",
        is_coupon: "",
        startTime: "",
        endTime: ""
      },
      temp: {
        id: undefined,
        name: "",
        ad_id: "",
        video_id: "",
        data: "",
        start_time: "",
        end_time: "",
        play_time: "",
        weight: 1,
        region_id: "",
        region_name: "",
        trade_id: "",
        trade_name: "",
        equipment_id: "",
        equipment_code: "",
        ad_word: "",
        describe: "",
        coupon_url: "",
        coupon_explain: ""
      },
      is_coupon: 2,
      temptime: [{ start: "", end: "" }],
      dialogFormVisible: false,
      dialogFormRead: false,
      dialogStatus: "",
      upLoading: false,
      visibleVideo: false,
      textMap: {
        update: "更新",
        create: "添加",
        review: "审核",
        read: "查看"
      },
      rules: {
        check_status: [
          { required: true, message: "请审核", trigger: "change" }
        ],
        check_remark: [{ required: true, trigger: "blur", validator: remark }]
      },
      createDisabled: true,
      updateDisabled: true,
      reviewDisabled: false,
      loading: false,
      industryOptions: [],
      areaOptions: [],
      areaCount: undefined,
      videoOptions: [],
      videoCount: undefined,
      industryCount: undefined,
      pageSize: 20,
      equipmentOptions: [],
      equipmentCount: undefined,
      permission: {
        add: false,
        del: false,
        update: false,
        review: false
      },
      pickerOptionsB: {
        // 限制时间
        // selectableRange: ['22:00:00 - 23:59:59','00:00:00 - 02:00:00'],
        // 限制日期
        disabledDate: this.disabledDateB
      },
      pickerOptionsE: {
        disabledDate: this.disabledDateE
      },
      pickerOptionsBT: {
        disabledDate: this.disabledDateBT
      },
      pickerOptionsET: {
        disabledDate: this.disabledDateET
      }
    };
  },
  computed: {
    operWidth: function() {
      let itemCount = 1;
      if (this.permission.del) {
        itemCount += 1;
      }
      if (this.permission.update) {
        itemCount += 1;
      }
      if (this.permission.review) {
        itemCount += 1;
      }
      return itemCount * 80 + "px";
    }
  },
  created() {
    if (this.$store.state.permission.jurisdiction === "*") {
      this.permission.add = true;
      this.permission.del = true;
      this.permission.update = true;
      this.permission.review = true;
    } else {
      const jurisdiction = this.$store.state.permission.jurisdiction.split(",");
      jurisdiction.forEach(element => {
        const id = parseInt(element);
        if (id === 162) {
          this.permission.add = true;
        } else if (id === 165) {
          this.permission.del = true;
        } else if (id === 164) {
          this.permission.update = true;
        } else if (id === 163) {
          this.permission.review = true;
        }
      });
    }
    this.type = JSON.parse(getToken()).type;
    this.getList();
  },
  methods: {
    disabledDateB(time) {
      if (this.listQuery.endTime) {
        return (
          time.getTime() >
          new Date(this.listQuery.endTime).getTime() + 24 * 3600 * 1000 - 8.64e7
        );
      }
    },
    disabledDateE(time) {
      if (this.listQuery.startTime) {
        return (
          time.getTime() < new Date(this.listQuery.startTime).getTime() - 8.64e6
        );
      }
    },
    disabledDateBT(time) {
      if (this.temp.end_time) {
        return (
          time.getTime() >
          new Date(this.temp.end_time).getTime() + 24 * 3600 * 1000 - 8.64e7
        );
      }
    },
    disabledDateET(time) {
      if (this.temp.start_time) {
        return (
          time.getTime() < new Date(this.temp.start_time).getTime() - 8.64e6
        );
      }
    },
    getVideoList(pageNumber = 1, search = "") {
      this.getvideoName(pageNumber, search);
    },
    getVideoid(data) {
      if (data.length > 0) {
        this.temp.video_id = data[0].id;
        this.temp.video_name = data[0].name;
      } else {
        this.temp.video_id = undefined;
        this.temp.video_name = "";
      }
    },
    getAreaList(pageNumber = 1, search = "") {
      this.getArea(pageNumber, search);
    },
    getAreaid(data) {
      if (data.length > 0) {
        this.temp.region_id = data[0].id;
        this.temp.region_name = data[0].name;
      } else {
        this.temp.region_id = undefined;
        this.temp.region_name = "";
      }
    },
    getIndustryList(pageNumber = 1, search = "") {
      this.getIndustry(pageNumber, search);
    },
    getEquipmentList(pageNumber = 1, search = "") {
      this.getEquipment(pageNumber, search);
    },
    getEquipmentid(data) {
      if (data.length > 0) {
        this.temp.equipment_id = data[0].id;
        this.temp.equipment_code = data[0].name;
      } else {
        this.temp.equipment_id = undefined;
        this.temp.equipment_code = "";
      }
    },
    getIndustryid(data) {
      if (data.length > 0) {
        this.temp.trade_id = data[0].id;
        this.temp.trade_name = data[0].name;
      } else {
        this.temp.trade_id = undefined;
        this.temp.trade_name = "";
      }
    },
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page
      };
      if (this.listQuery.name) {
        data["name"] = this.listQuery.name;
      }
      if (this.listQuery.ad_name) {
        data["ad_name"] = this.listQuery.ad_name;
      }
      if (this.listQuery.video_name) {
        data["video_name"] = this.listQuery.video_name;
      }
      if (this.listQuery.region_name) {
        data["region_name"] = this.listQuery.region_name;
      }
      if (this.listQuery.trade_name) {
        data["trade_name"] = this.listQuery.trade_name;
      }
      if (this.listQuery.equipment_code) {
        data["equipment_code"] = this.listQuery.equipment_code;
      }
      if (this.listQuery.check_status || this.listQuery.check_status === 0) {
        data["check_status"] = this.listQuery.check_status;
      }
      if (this.listQuery.is_coupon) {
        data["is_coupon"] = this.listQuery.is_coupon;
      }
      if (this.listQuery.startTime) {
        data["startTime"] = this.listQuery.startTime;
      }
      if (this.listQuery.endTime) {
        const date = new Date(this.listQuery.endTime);
        data["endTime"] = this.timestampToTime(
          date.getTime() + (60 * 60 * 24 * 1000 - 1)
        );
      }
      const adPlayRuleListPath = {
        // 获取规则列表
        company: "STQ",
        api: "pc_AdPlayRule_getRequestList",
        version: "1.0",
        data: data
      };
      this.socketApi.sendSock(JSON.stringify(adPlayRuleListPath), res => {
        if (res) {
          this.socketData(res);
          this.listLoading = false;
        } else {
          this.$message.error(" 获取视频源列表失败 ");
          this.listLoading = false;
        }
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleModifyStatus(row) {
      this.$confirm("此操作将永久删除该规则, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          const deleteRulePath = {
            company: "STQ",
            api: "pc_AdPlayRule_deleteRule",
            version: "1.0",
            data: {
              id: row.id
            }
          };
          this.socketApi.sendSock(JSON.stringify(deleteRulePath), res => {
            if (res) {
              this.socketData(res);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        name: "",
        ad_id: "",
        video_id: "",
        data: "",
        play_time: "",
        weight: 1,
        region_id: "",
        region_name: "",
        trade_id: "",
        trade_name: "",
        equipment_id: "",
        equipment_code: "",
        ad_word: "",
        describe: "",
        coupon_url: "",
        coupon_explain: ""
      };
      this.is_coupon = 2;
      this.temptime = [{ start: "", end: "" }];
    },
    handleRefresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        name: "",
        ad_name: "",
        video_name: "",
        region_name: "",
        trade_name: "",
        equipment_code: "",
        check_status: "",
        is_coupon: "",
        startTime: "",
        endTime: ""
      };
      this.getList();
    },
    handleAddRule(row, status) {
      const sessionData = {
        dialogStatus: status
      };
      if (row) {
        sessionData["id"] = row.id;
        sessionData["name"] = row.name;
      }
      if (row.check_status || row.check_status === 0) {
        sessionData["check_status"] = row.check_status;
      }
      sessionStorage.setItem("session", JSON.stringify(sessionData));
      this.$router.push({
        name: "playRule/addRule"
      });
    },
    getSelectRuleTime(start, end, idx) {
      this.temptime[idx].start = start;
      this.temptime[idx].end = end;
    },
    reduceCount(start, end, idx) {
      this.temptime.splice(idx, 1);
    },
    addDateTimeCount() {
      this.temptime.push({ start: "", end: "" });
    },
    handleReview(row) {
      // 打开审核弹窗
      this.temp = Object.assign({}, row); // copy obj
      this.temptime = JSON.parse(this.temp.play_time);
      if (this.temp.region_id) {
        this.temp.region_id = parseInt(this.temp.region_id);
      }
      if (this.temp.trade_id) {
        this.temp.trade_id = parseInt(this.temp.trade_id);
      }

      this.is_coupon = this.temp.is_coupon; // 放在外层变化触发灵敏

      this.dialogStatus = "review";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    handleRead(row) {
      // 打开查看弹窗
      this.temp = Object.assign({}, row); // copy obj

      this.is_coupon = this.temp.is_coupon; // 放在外层变化触发灵敏

      this.temptime = JSON.parse(this.temp.play_time);
      this.dialogStatus = "read";
      this.dialogFormRead = true;
    },
    reviewData() {
      // 审核规则信息
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          if (!this.reviewDisabled) {
            this.reviewDisabled = true;
            const data = {
              id: this.temp.id,
              check_status: this.temp.check_status
            };
            if (this.temp.check_remark) {
              data["check_remark"] = this.temp.check_remark;
            }
            const checkRulePath = {
              company: "STQ",
              api: "pc_AdPlayRule_checkRule",
              version: "1.0",
              data: data
            };
            this.socketApi.sendSock(JSON.stringify(checkRulePath), res => {
              if (res) {
                this.socketData(res);
              } else {
                this.$message.error(" 审核失败 ");
              }
              setTimeout(() => {
                this.reviewDisabled = false;
              }, 1000);
            });
          }
        }
      });
    },
    socketData(res) {
      if (res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
        } else {
          if (resj.api === "pc_AdPlayRule_getRequestList") {
            // 获取播放规则列表
            this.total = resj.count;
            this.list = resj.data;
            console.log("this.list", this.list);
          } else if (resj.api === "pc_AdPlayRule_checkRule") {
            // 播放规则审核
            this.dialogFormVisible = false;
            this.getList();
            this.$notify({
              title: "成功",
              message: "审核成功",
              type: "success",
              duration: 2000
            });
          } else if (resj.api === "pc_AdPlayRule_deleteRule") {
            // 规则删除
            this.getList();
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          }
        }
      }
    },
    // 时间转换
    getData(data) {
      var date = new Date(data);
      var Y = date.getFullYear();
      var M = date.getMonth() + 1;
      var D = date.getDate();
      return Y + "-" + M + "-" + D;
    },
    getRuleTime(data) {
      const time = [];
      data.forEach(res => {
        time.push(res.start + "-" + res.end);
      });
      return time.join(",");
    }
  }
};
</script>
